package kr.hkit.androidcomplete01.ch12;

import java.util.ArrayList;

import kr.hkit.androidcomplete01.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

//싱글 초이스 레이아웃 재활용..

public class ListAddDelMulti extends Activity {
	ArrayList<String> Items;
	ArrayAdapter<String> Adapter;
	ListView list;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_listadddelsinglechoice);
		
		Items = new ArrayList<String>();
		Items.add("First");
		Items.add("Second");
		Items.add("Third");
		Items.add("Fourth");
		Items.add("Fifth");
		
		Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,
											Items);
		list = (ListView) findViewById(R.id.adddellist);
		list.setAdapter(Adapter);
		list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
	}
	
	public void mOnClick(View v){
		EditText ed = (EditText) findViewById(R.id.newItem);
		switch(v.getId()){
		case R.id.btnAdd :
			String text = ed.getText().toString();
			if(text.length() != 0 ){
				Items.add(text);
				ed.setText("");
				Adapter.notifyDataSetChanged();
			}
			break;
		case R.id.btnDel:
			SparseBooleanArray sb = list.getCheckedItemPositions();
			if(sb.size() !=0){
				for(int i=list.getCount()-1; i>=0; i--){
					if(sb.get(i)){
						Items.remove(i);
					}
				}
				list.clearChoices();
				Adapter.notifyDataSetChanged();
			}
			break;
		}
		
	}
}
